/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package androidpos.gui;

import androidpos.db.ConnectToDB;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author user
 */
public class ProductPanel extends JPanel implements FocusListener, ItemListener {

    private int MAX_LIST = 4;
    private ArrayList<JComboBox> productComboList;
    private ArrayList<JLabel> priceFieldList;
    private ArrayList<JTextField> quanFieldList;
    private ArrayList<JLabel> totalList;
    private ArrayList<String> productList;
    private ArrayList<Double> priceList;
    private Connection connection;

    public ProductPanel() {
        connection = new ConnectToDB().getConnection();

        productList = new ArrayList<String>();
        priceList = new ArrayList<Double>();

        productComboList = new ArrayList<JComboBox>();
        priceFieldList = new ArrayList<JLabel>();
        quanFieldList = new ArrayList<JTextField>();
        totalList = new ArrayList<JLabel>();

        fillAllDataToArray();
        createForm();
        addAllListener();
    }

    private void addAllListener() {
        for (int i = 0; i < MAX_LIST; i++) {
            productComboList.get(i).addItemListener(this);
            quanFieldList.get(i).addFocusListener(this);
        }
    }

    private void fillAllDataToArray() {
        try {
            Statement st = connection.createStatement();
            ResultSet result = st.executeQuery("SELECT * FROM product ORDER BY id");
            while (result.next()) {
                productList.add(result.getString("name"));
                priceList.add(result.getDouble("price"));
            }
            for (int i = 0; i < MAX_LIST; i++) {
                productComboList.add(new JComboBox(productList.toArray()));
                priceFieldList.add(new JLabel(priceList.get(0) + ""));
                quanFieldList.add(new JTextField("0"));
                totalList.add(new JLabel("0.0"));
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public void createForm() {
        GridLayout layout = new GridLayout(MAX_LIST + 1, 2);
        layout.setHgap(20);
        setLayout(layout);
        add(new JLabel("Product"));
        add(new JLabel("Unit Price"));
        add(new JLabel("Quan."));
        add(new JLabel("Total"));
        for (int i = 0; i < MAX_LIST; i++) {
            add(productComboList.get(i));
            add(priceFieldList.get(i));
            add(quanFieldList.get(i));
            add(totalList.get(i));
        }
    }

    public void focusGained(FocusEvent e) {
    }

    public void focusLost(FocusEvent e) {
        JTextField quanField = (JTextField) e.getComponent();
        int index = quanFieldList.indexOf(quanField);
        fillTotalPrice(index);
    }

    private void fillTotalPrice(int i) {
        JLabel totalLabel = totalList.get(i);
        totalLabel.setText((Integer.parseInt(quanFieldList.get(i).getText()) * Double.parseDouble(priceFieldList.get(i).getText())) + "");
    }

    private void updateUnitPrice(JComboBox productCombo){
        int index = productComboList.indexOf(productCombo);
        int i = productCombo.getSelectedIndex();
        JLabel price = priceFieldList.get(index);
        price.setText(priceList.get(i)+"");
        fillTotalPrice(index);
    }
    public void itemStateChanged(ItemEvent e) {
        JComboBox productCombo = (JComboBox)e.getSource();
        updateUnitPrice(productCombo);
    }
    public double getTotalPrice(){
        double sum = 0;
        for(int i = 0 ; i < MAX_LIST ;i++){
            sum += Double.parseDouble(totalList.get(i).getText());
        }
        return sum;
    }
}
